<?php
/* Define a constant that can be checked to see if the component is installed or not. */
define ( 'BPSC_IS_INSTALLED', 1 );

/* Define a constant that will hold the current version number of the component */
define ( 'BPSC_VERSION', '0.11' );

load_plugin_textdomain( 'bpsc', false, '/bp-e-commerce/includes/languages/' );

if ( !defined( 'BPSC_GROUP_SLUG' ) )
	define ( 'BPSC_GROUP_SLUG', __( 'store', 'bpsc' ) );

if ( !defined( 'BPSC_PROFILE_SLUG' ) )
	define ( 'BPSC_PROFILE_SLUG', __( 'store', 'bpsc' ) );

if ( !defined( 'BPSC_PROFILE_SLUG_RECOMMENDATIONS' ) )
	define ( 'BPSC_PROFILE_SLUG_RECOMMENDATIONS', __( 'recommendations', 'bpsc' ) );


if ( !class_exists( 'WPSC_Query' ) )
	return;

require ( dirname( __FILE__ ) . '/bpsc-classes.php' );

/* The cssjs file should set up and enqueue all CSS and JS files used by the component */
require ( dirname( __FILE__ ) . '/bpsc-cssjs.php' );

/* The templatetags file should contain classes and functions designed for use in template files */
require ( dirname( __FILE__ ) . '/bpsc-templatetags.php' );

/* The filters file should create and apply filters to component output functions. */
require ( dirname( __FILE__ ) . '/bpsc-filters.php' );

/* Loads the Group extension */
if ( bp_is_active( 'groups' ) && bpsc_is_wpsc_active() ) {
	$settings = maybe_unserialize( get_blog_option( BP_ROOT_BLOG, 'bpsc' ) );
	if ( $settings['groupstoretoggle'] )
		require ( dirname( __FILE__ ) . '/bpsc-groupext.php' );
}

/**
 * bpsc_is_wpsc_active() 
 *
 * Convenience function for finding out if WordPress e-Commerce is active
 */
function bpsc_is_wpsc_active() {
	return defined( 'WPSC_VERSION' );
}

/**
 * bpsc_add_admin_menu()
 *
 * This function will add a WordPress wp-admin admin menu for your component under the
 * "BuddyPress" menu.
 */
function bpsc_add_admin_menu() {
	global $bp, $menu;

	if ( !$bp->loggedin_user->is_site_admin || !bpsc_is_wpsc_active() )
		return false;

	require ( dirname( __FILE__ ) . '/bpsc-admin.php' );

	add_action( 'load-store_page_wpsc-buddypress', 'bpsc_admin_pages_on_load' );
	add_action( 'admin_init', 'bpsc_admin_register_settings' );
	add_submenu_page( 'wpsc-sales-logs', __( 'BuddyPress Settings', 'bpsc' ), __( 'BuddyPress', 'bpsc' ), 'manage_options', 'wpsc-buddypress', 'bpsc_admin' );
}
add_action( 'admin_menu', 'bpsc_add_admin_menu', 11 );  /* wpsc_admin_pages = 10 */

/**
 * bpsc_shopping_cart_form_values()
 *
 * Automatically populates shopping cart form values (i.e. name, address) with user's xProfile data.
 */
function bpsc_shopping_cart_form_values() {
	global $bp, $wpdb, $wpsc_checkout;

	if ( !is_user_logged_in() || !bp_is_active( 'xprofile' ) /*|| isset( $_SESSION['wpsc_checkout_saved_values'] ) && !empty( $_SESSION['wpsc_checkout_saved_values'] )*/ )
		return;

	$settings = maybe_unserialize( get_blog_option( BP_ROOT_BLOG, 'bpsc' ) );
	if ( !$settings || !$settings['easycheckout'] )
		return;

	if ( !isset( $_SESSION['wpsc_checkout_saved_values'] ) )
		$_SESSION['wpsc_checkout_saved_values'] = array();

	// The settings are stored in a different format.
	$temp = array();
	foreach ( $settings['easycheckout'] as $setting ) {
		foreach ( $setting as $k => $v ) {
			if ( !$v )
				continue;

			$temp[$k] = $v;
		}
	}

	$settings = $temp;
	$country_codes = $wpdb->get_results( "SELECT isocode, country as name FROM " . WPSC_TABLE_CURRENCY_LIST, ARRAY_A );

	foreach ( $wpsc_checkout->checkout_items as $checkout_field ) {
		if ( !$checkout_field->unique_name )
			continue;

		foreach ( $settings as $setting_id => $setting_value ) {
			if ( $setting_value != $checkout_field->unique_name )
				continue;

			$field = new BP_XProfile_Field( $setting_id );
			$field_value = xprofile_get_field_data( $field->name, $bp->loggedin_user->id );

			if ( in_array( $field->type, array( 'textbox', 'selectbox' ) ) ) {

				if ( in_array( $checkout_field->unique_name, array( 'billingcountry', 'shippingcountry' ) ) ) {
					foreach ( $country_codes as $country ) {

						if ( $field_value == $country['name'] ) {
							if ( 'billingcountry' == $checkout_field->unique_name ) {
//								$_SESSION['wpsc_selected_region'] = $country['isocode'];
								$_SESSION['wpsc_selected_country'] = $country['isocode'];
								$field_value = '';
								break;

							} elseif ( 'shippingcountry' == $checkout_field->unique_name ) {  // TODO: I am not sure this works all the time
								$_SESSION['wpsc_delivery_country'] = $country['isocode'];
								$field_value = '';
								break;
							}
						}

					}
				}

			}

			if ( $field_value )
				$_SESSION['wpsc_checkout_saved_values'][$checkout_field->id] = $field_value;
		}
	}
}
add_action( 'wpsc_before_form_of_shopping_cart', 'bpsc_shopping_cart_form_values' );


/**
 * bpsc_load_template_filter()
 *
 * Custom load template filter; allows user to override these templates in
 * their active theme and replace the ones that are stored in the plugin directory.
 */
function bpsc_load_template_filter( $found_template, $templates ) {
	global $bp;

	if ( $bp->current_component != BPSC_PROFILE_SLUG )
		return $found_template;

	foreach ( (array) $templates as $template ) {
		if ( file_exists( STYLESHEETPATH . '/' . $template ) )
			$filtered_templates[] = STYLESHEETPATH . '/' . $template;
		else
			$filtered_templates[] = dirname( __FILE__ ) . '/templates/' . $template;
	}

	$found_template = $filtered_templates[0];
	return apply_filters( 'bpsc_load_template_filter', $found_template );
}
add_filter( 'bp_located_template', 'bpsc_load_template_filter', 10, 2 );

/**
 * bpsc_load_template()
 *
 * Loads templates within a template.
 */
function bpsc_load_template( $template ) {
	if ( $located_template = apply_filters( 'bp_located_template', locate_template( $template, false ), $template ) )
		load_template( apply_filters( 'bp_load_template', $located_template ) );
}


/********************************************************************************
 * Screen Functions
 *
 * Screen functions are the controllers of BuddyPress. They will execute when their
 * specific URL is caught. They will first save or manipulate data using business
 * functions, then pass on the user to a template file.
 */

function bpsc_setup_nav() {
	global $bp, $is_member_page;

	if ( !bp_is_my_profile() )
		return;

	$url = bp_get_root_domain() . '/' . BPSC_PROFILE_SLUG;
	bp_core_new_nav_item( array( 'name' => __( 'Store Recommendations', 'bpsc' ), 'slug' => BPSC_PROFILE_SLUG, 'screen_function' => 'bpsc_screen_shop_recommendations', 'default_subnav_slug' => BPSC_PROFILE_SLUG_RECOMMENDATIONS, 'item_css_id' => 'store-recommendations', 'show_for_displayed_user' => bp_is_my_profile() ) );
	bp_core_new_nav_default( array( 'parent_slug' => BPSC_PROFILE_SLUG, 'screen_function' => 'bpsc_screen_shop_recommendations', 'subnav_slug' => BPSC_PROFILE_SLUG_RECOMMENDATIONS ) );
}
//add_action( 'bp_setup_nav', 'bpsc_setup_nav' );

function bpsc_screen_shop_recommendations() {
	do_action( 'bpsc_screen_shop_recommendations' );
	bp_core_load_template( apply_filters( 'achievements_template_directory', 'bpsc/members/single/bpsc_recommendations' ) );
}
?>